package com.shd.edanpei.presenter;


import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.LendingBean;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.SureOrderContract;
import com.shd.edanpei.enums.StatusEnum;

import io.reactivex.android.schedulers.AndroidSchedulers;

public class SureOrderPresenter extends BasePresenterImpl<SureOrderContract.SureOrderIView>
        implements SureOrderContract.SureOrderIPresenter {

    public SureOrderPresenter(SureOrderContract.SureOrderIView mView) {
        super(mView);
    }

    @Override
    public void getLendingInfo(final int orderId) {
        mBaseIView.showLoading("");
        ApiCreator.getApi().getLendingInfo(orderId)
                .compose(RxTransformer.<LendingBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<LendingBean>() {
                    @Override
                    protected void onSuccess(LendingBean bean) {
                        mBaseIView.setLendingInfo(bean);
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        super.onFailure(throwable);
                        mBaseIView.getContext().finish();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        mBaseIView.stopLoading();
                    }
                });
    }

    @Override
    public void sureOrder(int orderId, final int index, int orderType) {
//        final int orderId = bundle.getInt(Constant.ORDER_ID);
//        final int orderType = bundle.getInt(Constant.TYPE);
//        final int position = bundle.getInt(Constant.INDEX);
//        final String amount = bundle.getString(Constant.AMOUNT);

        ApiCreator.getApi().sureOrder(orderId, orderType)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        RxBus.get().send(RxBusCode.REFRESH_ORDER_STATUS, new StatusBean(index, StatusEnum.waitfirm.getStatus()));
                        mBaseIView.getContext().finish();
                    }
                });

//
//        DialogUtil.showDialog5(mBaseIView.getContext(), new DialogUtil.DialogListener() {
//            @Override
//            public void sure() {
//                ApiCreator.getApi().sureOrder(orderId, orderType)
//                        .compose(RxTransformer.<NoDataResponse>errorHandle())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new BaseObserver<NoDataResponse>() {
//                            @Override
//                            protected void onSuccess(NoDataResponse data) {
//                                RxBus.get().send(RxBusCode.NOTIFY_ORDER_ADAPTER, new NotifyBean(position, StateEnum.received));
//                                mBaseIView.getContext().finish();
//                            }
//                        });
//            }
//        }, "确认放款", "确认本次操作放款" + amount + "万元", "确认");

    }
}
